package com.yntsoft.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.modules.web.Servlets;

import com.github.pagehelper.PageInfo;
import com.yntsoft.entity.Permission;
import com.yntsoft.entity.Role;
import com.yntsoft.entity.ShiroUser;
import com.yntsoft.service.PermissionService;
import com.yntsoft.service.RoleService;
import com.yntsoft.util.Constant;
import com.yntsoft.util.ResponCode;

/**
 * 角色控制层
 * @author Tony
 */
@Controller
@RequestMapping("role")
public class RoleController {

	@Autowired
	private RoleService roleService;
	
	@Autowired
	private PermissionService permissionService;
	
	/**
	 * 查询角色列表
	 * @return 
	 */
	@RequestMapping("rolelist")
	@RequiresPermissions("role:roleList")
	public String roleList (HttpServletRequest request,Model model){
		Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
		PageInfo<Role> roles = roleService.queryRoleList(searchParams,Constant.pageSize);
		model.addAttribute("pageInfo", roles);
		model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams, "search_"));	
 		return "role/rolePage";
	}
	
	
	/**
	 * 添加角色
	 * @return  
	 */
	@RequestMapping("addRole")
	@RequiresPermissions("role:addRole")
	public String saveRole (HttpServletRequest request,RedirectAttributes redirectAttributes,Role role){
		Integer rst = roleService.saveRole(role);
		if(rst == null || rst == 0 ){
			redirectAttributes.addFlashAttribute("message", "操作失败！");
		}else{
			redirectAttributes.addFlashAttribute("message", "操作成功！");
		}
 		return "redirect:/role/rolelist.do";
	}
	
	
	
	
	/**
	 * 添加角色
	 * @return  
	 */
	@RequestMapping("getRoleInfo")
	@ResponseBody
	@RequiresPermissions("role:getRoleInfo")
	public Map<String, Object> getRoleInfo (HttpServletRequest request,Integer roleId){
		Role rst = roleService.getRoleInfo(roleId);
		Map<String, Object> map = new HashMap<String,Object>();
		if(rst != null){
			map.put("code", ResponCode.code_success);
			map.put("role", rst);
		}else{
			map.put("code", ResponCode.code_fail);
		}
 		return map;
	}
	
	/**
	 * 获取用户信息
	 * @param request
	 * @param user
	 * @param attributes
	 * @return   
	 */
	@RequestMapping("roleDel")
	@RequiresPermissions("role:roleDel")
	public String roleDel(HttpServletRequest request,RedirectAttributes redirectAttributes,String ids){
		if (ids != null && ids!="") {
			Integer rst = roleService.roleDel(ids);
			if(rst != null && rst != 0){
				redirectAttributes.addFlashAttribute("message", "删除成功");
			}else{
				redirectAttributes.addFlashAttribute("message", "删除失败");
			}
		}else{
			redirectAttributes.addFlashAttribute("message", "删除失败");
		}
		return "redirect:/role/rolelist.do";
	}
	
	
	
	@RequestMapping("getRolePermission")
	@RequiresPermissions("role:getRolePermission")
	@ResponseBody
	public Map<String, Object> getRolePermission(HttpServletRequest request,String roleId){
		Map<String, Object> map = new HashMap<String, Object>();
		List<Permission>  rolePermissions= permissionService.getRolePermission(roleId);
		List<Permission> permissionList = permissionService.getPermissionList();
		
		for (int j = 0; j < rolePermissions.size(); j++) {
			Permission rolePermission = rolePermissions.get(j);
			for (int i = 0; i < permissionList.size(); i++) {
				Permission permission = permissionList.get(i);
				if(permission.getId() .equals(rolePermission.getId())){
					permissionList.remove(i);
					break;
				}
			}
		}
		
		map.put("rolePermissions", rolePermissions);
		map.put("permissionList", permissionList);
		return map;
	}
	
	
	

	@RequestMapping("saveRolePermission")
	@RequiresPermissions("role:saveRolePermission")
	@ResponseBody
	public Map<String, Object> saveRolePermission(HttpServletRequest request,String permissionIds,String roleId){
		Map<String, Object> map = new HashMap<String,Object>();
		Integer rst = permissionService.saveRolePermission(permissionIds,roleId);
		if(rst != 0){
			map.put("code",ResponCode.code_success);
		}else{
			map.put("code",ResponCode.code_fail);
		}
		return map;
	}
	
}
